
import RPi.GPIO as GPIO
import time

# Define GPIO pins for 28BYJ-48 and ULN2003 driver in GPIO.BCM
IN1 = 26
IN2 = 16
IN3 = 7
IN4 = 8

GPIO.setwarnings(False)
# Set GPIO mode and configure pins
GPIO.setmode(GPIO.BCM)
GPIO.setup(IN1, GPIO.OUT)
GPIO.setup(IN2, GPIO.OUT)
GPIO.setup(IN3, GPIO.OUT)
GPIO.setup(IN4, GPIO.OUT)

# Define constants
DEG_PER_STEP = 1.8
STEPS_PER_REVOLUTION = int(360 / DEG_PER_STEP)

# Function to move the stepper motor one step anticlockwise
def step_anticlockwise(delay, steps):
    for _ in range(steps):
        for coil in [IN1,IN2,IN3,IN4]:
           GPIO.output(coil, GPIO.HIGH)
           time.sleep(delay)
           GPIO.output(coil, GPIO.LOW)

# Function to move the stepper motor one step clockwise
def step_clockwise(delay, steps):
    for _ in range(steps):
        for coil in [IN4,IN3,IN2,IN1]:
           GPIO.output(coil, GPIO.HIGH)
           time.sleep(delay)
           GPIO.output(coil, GPIO.LOW)

try:
    # Set the delay between steps
    delay = 0.005
    while True:
        # Rotate one revolution anticlockwise
        step_anticlockwise(delay, STEPS_PER_REVOLUTION)
        time.sleep(1)

        # Rotate one revolution clockwise
        step_clockwise(delay, STEPS_PER_REVOLUTION)
        time.sleep(1)

except KeyboardInterrupt:
    print("\nExiting the script.")

finally:
    # Clean up GPIO settings
    GPIO.cleanup()
